home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / ProgressiveMesh / progressivemesh.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  25.0 KB  |  711 lines

  1. //-----------------------------------------------------------------------------
  2. // File: ProgressiveMesh.cpp
  3. //
  4. // Desc: Sample of creating progressive meshes in D3D
  5. //
  6. // Copyright (c) 1998-2001 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #include <stdio.h>
  10. #include <windows.h>
  11. #include <commdlg.h>
  12. #include <D3DX8.h>
  13. #include "D3DApp.h"
  14. #include "D3DFont.h"
  15. #include "D3DUtil.h"
  16. #include "DXUtil.h"
  17. #include "resource.h"
  18.  
  19.  
  20.  
  21.  
  22. //-----------------------------------------------------------------------------
  23. // Name: class CMyD3DApplication
  24. // Desc: Main class to run this application. Most functionality is inherited
  25. //       from the CD3DApplication base class.
  26. //-----------------------------------------------------------------------------
  27. class CMyD3DApplication : public CD3DApplication
  28. {
  29.     TCHAR               m_strInitialDir[512];
  30.     TCHAR               m_strMeshFilename[512]; // Filename of mesh
  31.     LPD3DXPMESH*        m_pPMeshes;          
  32.     LPD3DXPMESH         m_pPMeshFull;          
  33.     DWORD               m_cPMeshes;
  34.     DWORD               m_iPMeshCur;
  35.  
  36.     D3DMATERIAL8*       m_mtrlMeshMaterials;
  37.     LPDIRECT3DTEXTURE8* m_pMeshTextures;        // Array of textures, entries are NULL if no texture specified
  38.     DWORD               m_dwNumMaterials;       // Number of materials
  39.  
  40.     CD3DArcBall         m_ArcBall;              // Mouse rotation utility
  41.     D3DXVECTOR3         m_vObjectCenter;        // Center of bounding sphere of object
  42.     FLOAT               m_fObjectRadius;        // Radius of bounding sphere of object
  43.  
  44.     CD3DFont*           m_pFont;                // Font for displaying help
  45.     BOOL                m_bDisplayHelp;
  46.     BOOL                m_bShowOptimized;
  47.  
  48. public:
  49.     HRESULT OneTimeSceneInit();
  50.     HRESULT InitDeviceObjects();
  51.     HRESULT RestoreDeviceObjects();
  52.     HRESULT InvalidateDeviceObjects();
  53.     HRESULT DeleteDeviceObjects();
  54.     HRESULT Render();
  55.     HRESULT FrameMove();
  56.     HRESULT FinalCleanup();
  57.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  58.     void SetNumVertices(DWORD dwNumMeshVertices);
  59.  
  60.     CMyD3DApplication();
  61. };
  62.  
  63.  
  64.  
  65.  
  66. //-----------------------------------------------------------------------------
  67. // Name: WinMain()
  68. // Desc: Entry point to the program. Initializes everything, and goes into a
  69. //       message-processing loop. Idle time is used to render the scene.
  70. //-----------------------------------------------------------------------------
  71. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  72. {
  73.     CMyD3DApplication d3dApp;
  74.  
  75.     if( FAILED( d3dApp.Create( hInst ) ) )
  76.         return 0;
  77.  
  78.     return d3dApp.Run();
  79. }
  80.  
  81.  
  82.  
  83.  
  84. //-----------------------------------------------------------------------------
  85. // Name: CMyD3DApplication()
  86. // Desc: Constructor
  87. //-----------------------------------------------------------------------------
  88. CMyD3DApplication::CMyD3DApplication()
  89. {
  90.     // Override base class members
  91.     m_strWindowTitle    = _T("ProgressiveMesh: Using Progressive Meshes in D3D");
  92.     m_bUseDepthBuffer   = TRUE;
  93.     m_bShowCursorWhenFullscreen = TRUE;
  94.  
  95.     // Initialize member variables
  96.     _tcscpy( m_strInitialDir, DXUtil_GetDXSDKMediaPath() );
  97.     _tcscpy( m_strMeshFilename, _T("Tiger.x") );
  98.  
  99.     m_pPMeshes           = NULL;
  100.     m_cPMeshes           = 0;
  101.     m_pPMeshFull         = NULL;
  102.     m_bShowOptimized     = TRUE;
  103.  
  104.     m_dwNumMaterials     = 0L;
  105.     m_mtrlMeshMaterials  = NULL;
  106.     m_pMeshTextures      = NULL;
  107.  
  108.     m_pFont              = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  109.     m_bDisplayHelp       = FALSE;
  110. }
  111.  
  112.  
  113.  
  114.  
  115. //-----------------------------------------------------------------------------
  116. // Name: OneTimeSceneInit()
  117. // Desc: Called during initial app startup, this function performs all the
  118. //       permanent initialization.
  119. //-----------------------------------------------------------------------------
  120. HRESULT CMyD3DApplication::OneTimeSceneInit()
  121. {
  122.     // Set cursor to indicate that user can move the object with the mouse
  123. #ifdef _WIN64
  124.     SetClassLongPtr( m_hWnd, GCLP_HCURSOR, (LONG_PTR)LoadCursor( NULL, IDC_SIZEALL ) );
  125. #else
  126.     SetClassLong( m_hWnd, GCL_HCURSOR, (LONG)LoadCursor( NULL, IDC_SIZEALL ) );
  127. #endif
  128.     return S_OK;
  129. }
  130.  
  131.  
  132.  
  133.  
  134. //-----------------------------------------------------------------------------
  135. // Name: FrameMove()
  136. // Desc: Called once per frame, the call is the entry point for animating
  137. //       the scene.
  138. //-----------------------------------------------------------------------------
  139. HRESULT CMyD3DApplication::FrameMove()
  140. {
  141.     // Setup world matrix
  142.     D3DXMATRIX matWorld;
  143.     D3DXMatrixTranslation( &matWorld, -m_vObjectCenter.x,
  144.                                       -m_vObjectCenter.y,
  145.                                       -m_vObjectCenter.z );
  146.     D3DXMatrixMultiply( &matWorld, &matWorld, m_ArcBall.GetRotationMatrix() );
  147.     D3DXMatrixMultiply( &matWorld, &matWorld, m_ArcBall.GetTranslationMatrix() );
  148.     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  149.  
  150.     // Set up view matrix
  151.     D3DXMATRIX matView;
  152.     D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0, 0,-3*m_fObjectRadius ),
  153.                                   &D3DXVECTOR3( 0, 0, 0 ),
  154.                                   &D3DXVECTOR3( 0, 1, 0 ) );
  155.     m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
  156.  
  157.     return S_OK;
  158. }
  159.  
  160.  
  161.  
  162.  
  163. //-----------------------------------------------------------------------------
  164. // Name: Render()
  165. // Desc: Called once per frame, the call is the entry point for 3d
  166. //       rendering. This function sets up render states, clears the
  167. //       viewport, and renders the scene.
  168. //-----------------------------------------------------------------------------
  169. HRESULT CMyD3DApplication::Render()
  170. {
  171.     // Clear the backbuffer
  172.     m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  173.                          0x000000ff, 1.0f, 0x00000000 );
  174.  
  175.     // Begin scene rendering
  176.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  177.     {
  178.         if( m_pPMeshes )
  179.         {
  180.             // Set and draw each of the materials in the mesh
  181.             for( DWORD i=0; i<m_dwNumMaterials; i++ )
  182.             {
  183.                 m_pd3dDevice->SetMaterial( &m_mtrlMeshMaterials[i] );
  184.                 m_pd3dDevice->SetTexture( 0, m_pMeshTextures[i] );
  185.                 if (m_bShowOptimized)
  186.                     m_pPMeshes[m_iPMeshCur]->DrawSubset( i );
  187.                 else
  188.                     m_pPMeshFull->DrawSubset( i );
  189.             }
  190.         }
  191.  
  192.  
  193.         // Output statistics
  194.         m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  195.         m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  196.         TCHAR strBuffer[512];
  197.         if (m_bShowOptimized)
  198.         {
  199.             if (!m_pPMeshes)
  200.             {
  201.                 sprintf( strBuffer, _T("Unoptimized") );
  202.                 m_pFont->DrawText( 2, 60, 0xffffff00, strBuffer );
  203.  
  204.                 sprintf( strBuffer, _T("Num Vertices = %ld, Optimized %f"),
  205.                          0, 0.0f);
  206.                 m_pFont->DrawText( 2, 40, 0xffffff00, strBuffer );
  207.             }
  208.             else
  209.             {
  210.                 sprintf( strBuffer, _T("PMesh %d out of %d"), m_iPMeshCur + 1, m_cPMeshes );
  211.                 m_pFont->DrawText( 2, 60, 0xffffff00, strBuffer );
  212.  
  213.                 sprintf( strBuffer, _T("Num Vertices = %ld, Min = %ld, Max = %ld%"),
  214.                          m_pPMeshes[m_iPMeshCur]->GetNumVertices(),
  215.                          m_pPMeshes[m_iPMeshCur]->GetMinVertices(),
  216.                          m_pPMeshes[m_iPMeshCur]->GetMaxVertices());
  217.                 m_pFont->DrawText( 2, 40, 0xffffff00, strBuffer );
  218.             }
  219.         }
  220.         else
  221.         {
  222.             sprintf( strBuffer, _T("Unoptimized") );
  223.             m_pFont->DrawText( 2, 60, 0xffffff00, strBuffer );
  224.  
  225.             sprintf( strBuffer, _T("Num Vertices = %ld, Min = %ld, Max = %ld%"),
  226.                      m_pPMeshFull->GetNumVertices(),
  227.                      m_pPMeshFull->GetMinVertices(),
  228.                      m_pPMeshFull->GetMaxVertices());
  229.             m_pFont->DrawText( 2, 40, 0xffffff00, strBuffer );
  230.         }
  231.  
  232.         // Output text
  233.         if( m_bDisplayHelp )
  234.         {
  235.             m_pFont->DrawText(  2, 80, 0xffffffff, _T("<F1>\n\n")
  236.                                                    _T("<Up>\n")
  237.                                                    _T("<Down>\n\n")
  238.                                                    _T("<PgUp>\n")
  239.                                                    _T("<PgDn>\n\n")
  240.                                                    _T("<Home>\n")
  241.                                                    _T("<End>\n")
  242.                                                    _T("<o>"), 0L );
  243.             m_pFont->DrawText( 70, 80, 0xffffffff, _T("Toggle help\n\n")
  244.                                                    _T("Add one vertex\n")
  245.                                                    _T("Subtract one vertex\n\n")
  246.                                                    _T("Add 100 vertices\n")
  247.                                                    _T("Subtract 100 vertices\n\n")
  248.                                                    _T("Max vertices\n")
  249.                                                    _T("Min vertices\n")
  250.                                                    _T("Show optimized pmeshes") );
  251.         }
  252.         else
  253.         {
  254.             m_pFont->DrawText(  2, 80, 0xffffffff, _T("<F1>\n") );
  255.             m_pFont->DrawText( 70, 80, 0xffffffff, _T("Toggle help\n") );
  256.         }
  257.  
  258.         // End the scene rendering
  259.         m_pd3dDevice->EndScene();
  260.     }
  261.  
  262.     return S_OK;
  263. }
  264.  
  265.  
  266.  
  267.  
  268. //-----------------------------------------------------------------------------
  269. // Name: InitDeviceObjects()
  270. // Desc: Initialize scene objects.
  271. //-----------------------------------------------------------------------------
  272. HRESULT CMyD3DApplication::InitDeviceObjects()
  273. {
  274.     DWORD cVerticesPerMesh;
  275.  
  276.     // Initialize the font
  277.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  278.  
  279.     CheckMenuItem( GetMenu(m_hWnd), ID_OPTIONS_SHOWOPTIMIZEDPMESHES, m_bShowOptimized ? MF_CHECKED : MF_UNCHECKED );
  280.  
  281.     // Load mesh
  282.     LPD3DXBUFFER pAdjacencyBuffer = NULL;
  283.     LPDIRECT3DVERTEXBUFFER8 pVertexBuffer = NULL;
  284.     LPD3DXMESH   pMesh = NULL;
  285.     LPD3DXPMESH  pPMesh = NULL;
  286.     LPD3DXMESH   pTempMesh;
  287.     LPD3DXBUFFER pD3DXMtrlBuffer = NULL;
  288.     BYTE*        pVertices;
  289.     TCHAR        strMediaPath[512];
  290.     HRESULT      hr;
  291.     DWORD        dw32BitFlag;
  292.     DWORD        cVerticesMin;
  293.     DWORD        cVerticesMax;
  294.     DWORD        iPMesh;
  295.     D3DXWELDEPSILONS Epsilons;
  296.     DWORD        i;
  297.     D3DXMATERIAL* d3dxMaterials;
  298.  
  299.     // Find the path to the mesh
  300.     if( FAILED( DXUtil_FindMediaFile( strMediaPath, m_strMeshFilename ) ) )
  301.         return D3DAPPERR_MEDIANOTFOUND;
  302.  
  303.     // Load the mesh from the specified file
  304.     if( FAILED( hr = D3DXLoadMeshFromX( strMediaPath, D3DXMESH_MANAGED, m_pd3dDevice,
  305.                                         &pAdjacencyBuffer, &pD3DXMtrlBuffer,
  306.                                         &m_dwNumMaterials, &pMesh ) ) )
  307.     {
  308.         // hide error so that device changes will not cause exit, shows blank screen instead
  309.         goto End;
  310.     }
  311.  
  312.     dw32BitFlag = (pMesh->GetOptions() & D3DXMESH_32BIT);
  313.  
  314.     // perform simple cleansing operations on mesh
  315.     if( FAILED( hr = D3DXCleanMesh( pMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), &pTempMesh, 
  316.                                            (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL ) ) )
  317.     {
  318.         m_dwNumMaterials = 0;
  319.         goto End;
  320.     }
  321.     SAFE_RELEASE(pMesh);
  322.     pMesh = pTempMesh;
  323.  
  324.     //  Perform a weld to try and remove excess vertices like the model bigship1.x in the DX8.0 SDK (current model is fixed)
  325.     //    Weld the mesh using all epsilons of 0.0f.  A small epsilon like 1e-6 works well too
  326.     memset(&Epsilons, 0, sizeof(D3DXWELDEPSILONS));
  327.     if( FAILED( hr = D3DXWeldVertices( pMesh, &Epsilons, 
  328.                                                 (DWORD*)pAdjacencyBuffer->GetBufferPointer(), 
  329.                                                 (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL ) ) )
  330.     {
  331.         m_dwNumMaterials = 0;
  332.         goto End;
  333.     }
  334.  
  335.     // verify validity of mesh for simplification
  336.     if( FAILED( hr = D3DXValidMesh( pMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL ) ) )
  337.     {
  338.         m_dwNumMaterials = 0;
  339.         goto End;
  340.     }
  341.  
  342.     // Allocate a material/texture arrays
  343.     d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
  344.     m_mtrlMeshMaterials = new D3DMATERIAL8[m_dwNumMaterials];
  345.     m_pMeshTextures     = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
  346.  
  347.     // Copy the materials and load the textures
  348.     for( i=0; i<m_dwNumMaterials; i++ )
  349.     {
  350.         m_mtrlMeshMaterials[i] = d3dxMaterials[i].MatD3D;
  351.         m_mtrlMeshMaterials[i].Ambient = m_mtrlMeshMaterials[i].Diffuse;
  352.  
  353.         // Find the path to the texture and create that texture
  354.         DXUtil_FindMediaFile( strMediaPath, d3dxMaterials[i].pTextureFilename );
  355.         if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, strMediaPath, 
  356.                                                &m_pMeshTextures[i] ) ) )
  357.             m_pMeshTextures[i] = NULL;
  358.     }
  359.     pD3DXMtrlBuffer->Release();
  360.     pD3DXMtrlBuffer = NULL;
  361.  
  362.  
  363.     // Lock the vertex buffer, to generate a simple bounding sphere
  364.     hr = pMesh->GetVertexBuffer( &pVertexBuffer );
  365.     if( FAILED(hr) )
  366.         goto End;
  367.  
  368.     hr = pVertexBuffer->Lock( 0, 0, &pVertices, D3DLOCK_NOSYSLOCK );
  369.     if( FAILED(hr) )
  370.         goto End;
  371.  
  372.     hr = D3DXComputeBoundingSphere( pVertices, pMesh->GetNumVertices(),
  373.                                     pMesh->GetFVF(),
  374.                                     &m_vObjectCenter, &m_fObjectRadius );
  375.     pVertexBuffer->Unlock();
  376.     pVertexBuffer->Release();
  377.  
  378.     if( FAILED(hr) || m_dwNumMaterials == 0 )
  379.         goto End;
  380.  
  381.     if ( !(pMesh->GetFVF() & D3DFVF_NORMAL) )
  382.     {
  383.         hr = pMesh->CloneMeshFVF( dw32BitFlag|D3DXMESH_MANAGED, pMesh->GetFVF() | D3DFVF_NORMAL, 
  384.                                             m_pd3dDevice, &pTempMesh );
  385.         if (FAILED(hr))
  386.             goto End;
  387.  
  388.         D3DXComputeNormals( pTempMesh, NULL );
  389.  
  390.         pMesh->Release();
  391.         pMesh = pTempMesh;
  392.     }
  393.  
  394.     hr = D3DXGeneratePMesh( pMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer(),
  395.                             NULL, NULL, 1, D3DXMESHSIMP_VERTEX, &pPMesh);
  396.     if( FAILED(hr) )
  397.         goto End;
  398.  
  399.     cVerticesMin = pPMesh->GetMinVertices();
  400.     cVerticesMax = pPMesh->GetMaxVertices();
  401.  
  402.     cVerticesPerMesh = (cVerticesMax - cVerticesMin) / 10;
  403.  
  404.     m_cPMeshes = max(1, (DWORD)ceil((cVerticesMax - cVerticesMin) / (float)cVerticesPerMesh));
  405.     m_pPMeshes = new LPD3DXPMESH[m_cPMeshes];
  406.     if (m_pPMeshes == NULL)
  407.     {
  408.         hr = E_OUTOFMEMORY;
  409.         goto End;
  410.     }
  411.     memset(m_pPMeshes, 0, sizeof(LPD3DXPMESH) * m_cPMeshes);
  412.  
  413.     // clone full size pmesh
  414.     hr = pPMesh->ClonePMeshFVF( D3DXMESH_MANAGED | D3DXMESH_VB_SHARE, pPMesh->GetFVF(), m_pd3dDevice, &m_pPMeshFull );
  415.     if (FAILED(hr))
  416.         goto End;
  417.  
  418.     // clone all the separate pmeshes
  419.     for (iPMesh = 0; iPMesh < m_cPMeshes; iPMesh++)
  420.     {
  421.         hr = pPMesh->ClonePMeshFVF( D3DXMESH_MANAGED | D3DXMESH_VB_SHARE, pPMesh->GetFVF(), m_pd3dDevice, &m_pPMeshes[iPMesh] );
  422.         if (FAILED(hr))
  423.             goto End;
  424.  
  425.         // trim to appropriate space
  426.         hr = m_pPMeshes[iPMesh]->TrimByVertices(cVerticesMin + cVerticesPerMesh * iPMesh, cVerticesMin + cVerticesPerMesh * (iPMesh+1), NULL, NULL);
  427.         if (FAILED(hr))
  428.             goto End;
  429.  
  430.         hr = m_pPMeshes[iPMesh]->OptimizeBaseLOD(D3DXMESHOPT_VERTEXCACHE, NULL);
  431.         if (FAILED(hr))
  432.             goto End;
  433.     }
  434.  
  435.     // set current to be maximum number of vertices
  436.     m_iPMeshCur = m_cPMeshes - 1;
  437.     hr = m_pPMeshes[m_iPMeshCur]->SetNumVertices(cVerticesMax);
  438.     if (FAILED(hr))
  439.         goto End;
  440.  
  441.     hr = m_pPMeshFull->SetNumVertices(cVerticesMax);
  442.     if (FAILED(hr))
  443.         goto End;
  444. End:
  445.     SAFE_RELEASE( pAdjacencyBuffer );
  446.     SAFE_RELEASE( pD3DXMtrlBuffer );
  447.     SAFE_RELEASE( pMesh );
  448.     SAFE_RELEASE( pPMesh );
  449.  
  450.     if (FAILED(hr))
  451.     {
  452.         for (iPMesh = 0; iPMesh < m_cPMeshes; iPMesh++)
  453.         {
  454.             SAFE_RELEASE( m_pPMeshes[iPMesh] );
  455.         }
  456.  
  457.         delete []m_pPMeshes;
  458.         m_cPMeshes = 0;
  459.         m_pPMeshes = NULL;
  460.         SAFE_RELEASE( m_pPMeshFull )
  461.     }
  462.  
  463.     return hr;
  464. }
  465.  
  466.  
  467.  
  468.  
  469. //-----------------------------------------------------------------------------
  470. // Name: RestoreDeviceObjects()
  471. // Desc: Initialize scene objects.
  472. //-----------------------------------------------------------------------------
  473. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  474. {
  475.     m_pFont->RestoreDeviceObjects();
  476.  
  477.     // Setup render state
  478.     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     TRUE );
  479.     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
  480.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
  481.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  482.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  483.  
  484.     // Setup the light
  485.     D3DLIGHT8 light;
  486.     D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f, -0.5f, 1.0f );
  487.     m_pd3dDevice->SetLight( 0, &light );
  488.     m_pd3dDevice->LightEnable( 0, TRUE );
  489.     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00333333 );
  490.  
  491.     // Set the arcball parameters
  492.     m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height,
  493.                          0.85f );
  494.     m_ArcBall.SetRadius( m_fObjectRadius );
  495.  
  496.     // Set the projection matrix
  497.     D3DXMATRIX matProj;
  498.     FLOAT      fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
  499.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, m_fObjectRadius/64.0f,
  500.                                 m_fObjectRadius*200.0f);
  501.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  502.  
  503.     return S_OK;
  504. }
  505.  
  506.  
  507.  
  508.  
  509. //-----------------------------------------------------------------------------
  510. // Name: InvalidateDeviceObjects()
  511. // Desc: 
  512. //-----------------------------------------------------------------------------
  513. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  514. {
  515.     m_pFont->InvalidateDeviceObjects();
  516.  
  517.     return S_OK;
  518. }
  519.  
  520.  
  521.  
  522.  
  523. //-----------------------------------------------------------------------------
  524. // Name: DeleteDeviceObjects()
  525. // Desc: Called when the app is exiting, or the device is being changed,
  526. //       this function deletes any device dependent objects.
  527. //-----------------------------------------------------------------------------
  528. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  529. {
  530.     DWORD iPMesh;
  531.  
  532.     m_pFont->DeleteDeviceObjects();
  533.  
  534.     if( m_pMeshTextures != NULL)
  535.     {
  536.         for( UINT i=0; i<m_dwNumMaterials; i++ )
  537.             SAFE_RELEASE( m_pMeshTextures[i] );
  538.     }
  539.     SAFE_DELETE_ARRAY( m_pMeshTextures );
  540.  
  541.     SAFE_RELEASE( m_pPMeshFull );
  542.     for (iPMesh = 0; iPMesh < m_cPMeshes; iPMesh++)
  543.     {
  544.         SAFE_RELEASE( m_pPMeshes[iPMesh] );
  545.     }
  546.     m_cPMeshes = 0;
  547.     delete []m_pPMeshes;
  548.  
  549.     SAFE_DELETE_ARRAY( m_mtrlMeshMaterials );
  550.     m_dwNumMaterials = 0L;
  551.  
  552.     return S_OK;
  553. }
  554.  
  555.  
  556.  
  557.  
  558. //-----------------------------------------------------------------------------
  559. // Name: FinalCleanup()
  560. // Desc: Called during initial app startup, this function performs all the
  561. //       permanent initialization.
  562. //-----------------------------------------------------------------------------
  563. HRESULT CMyD3DApplication::FinalCleanup()
  564. {
  565.     SAFE_DELETE( m_pFont );
  566.  
  567.     return S_OK;
  568. }
  569.  
  570.  
  571.  
  572.  
  573. //-----------------------------------------------------------------------------
  574. // Name: MsgProc()
  575. // Desc: Message proc function to handle key and menu input
  576. //-----------------------------------------------------------------------------
  577. LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  578.                                     LPARAM lParam )
  579. {
  580.     // Pass mouse messages to the ArcBall so it can build internal matrices
  581.     m_ArcBall.HandleMouseMessages( hWnd, uMsg, wParam, lParam );
  582.  
  583.     // Trap the context menu
  584.     if( uMsg == WM_CONTEXTMENU )
  585.         return 0;
  586.  
  587.     if( uMsg == WM_COMMAND )
  588.     {
  589.         // Handle the about command (which displays help)
  590.         if( LOWORD(wParam) == IDM_TOGGLEHELP )
  591.         {
  592.             m_bDisplayHelp = !m_bDisplayHelp;
  593.             return 0;
  594.         }
  595.         else if ( LOWORD(wParam) == ID_OPTIONS_SHOWOPTIMIZEDPMESHES )
  596.         {
  597.             m_bShowOptimized = !m_bShowOptimized;
  598.             CheckMenuItem( GetMenu(hWnd), ID_OPTIONS_SHOWOPTIMIZEDPMESHES, m_bShowOptimized ? MF_CHECKED : MF_UNCHECKED );
  599.         }
  600.  
  601.  
  602.         // Handle the open file command
  603.         if( m_bWindowed && LOWORD(wParam) == IDM_OPENFILE )
  604.         {
  605.             TCHAR g_strFilename[512]   = _T("");
  606.  
  607.             // Display the OpenFileName dialog. Then, try to load the specified file
  608.             OPENFILENAME ofn = { sizeof(OPENFILENAME), NULL, NULL,
  609.                                 _T(".X Files (.x)\0*.x\0\0"), 
  610.                                 NULL, 0, 1, m_strMeshFilename, 512, g_strFilename, 512, 
  611.                                 m_strInitialDir, _T("Open Mesh File"), 
  612.                                 OFN_FILEMUSTEXIST, 0, 1, NULL, 0, NULL, NULL };
  613.  
  614.             if( TRUE == GetOpenFileName( &ofn ) )
  615.             {
  616.                 _tcscpy( m_strInitialDir, m_strMeshFilename );
  617.                 TCHAR* pLastSlash =  _tcsrchr( m_strInitialDir, _T('\\') );
  618.                 if( pLastSlash )
  619.                     *pLastSlash = 0;
  620.                 SetCurrentDirectory( m_strInitialDir );
  621.  
  622.                 // Destroy and recreate everything
  623.                 InvalidateDeviceObjects();
  624.                 DeleteDeviceObjects();
  625.                 
  626.                 if( FAILED( InitDeviceObjects() ) )
  627.                 {
  628.                     MessageBox( m_hWnd, _T("Error loading mesh: mesh may not\n")
  629.                                         _T("be valid. See debug output for\n")
  630.                                         _T("more information.\n\nPlease select ") 
  631.                                         _T("a different .x file."), 
  632.                                         _T("ProgressiveMesh"), MB_ICONERROR|MB_OK );
  633.                 }
  634.  
  635.                 RestoreDeviceObjects();
  636.             }
  637.         }
  638.     }
  639.  
  640.  
  641.     if( uMsg == WM_KEYDOWN )
  642.     {
  643.         if( m_pPMeshes )
  644.         {
  645.             DWORD dwNumMeshVertices = m_pPMeshes[m_iPMeshCur]->GetNumVertices();
  646.  
  647.             if( wParam == VK_UP )
  648.             {
  649.                 // Sometimes it is necessary to add more than one
  650.                 // vertex when increasing the resolution of a 
  651.                 // progressive mesh, so keep adding until the 
  652.                 // vertex count increases.
  653.                 for( int i = 1; i <= 8; i++ )
  654.                 {
  655.                     SetNumVertices( dwNumMeshVertices+i );
  656.                     if( m_pPMeshes[m_iPMeshCur]->GetNumVertices() == dwNumMeshVertices+i )
  657.                         break;
  658.                 }
  659.             }
  660.             else if( wParam == VK_DOWN )
  661.                 SetNumVertices( dwNumMeshVertices-1 );
  662.             else if( wParam == VK_PRIOR )
  663.                 SetNumVertices( dwNumMeshVertices+100 );
  664.             else if( wParam == VK_NEXT )
  665.                 SetNumVertices( dwNumMeshVertices<=100 ? 1 : dwNumMeshVertices-100 );
  666.             else if( wParam == VK_HOME )
  667.                 SetNumVertices( 0xffffffff );
  668.             else if( wParam == VK_END )
  669.                 SetNumVertices( 1 );
  670.         }
  671.     }
  672.  
  673.     return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
  674. }
  675.  
  676.  
  677.  
  678. //-----------------------------------------------------------------------------
  679. // Name: SetNumVertices()
  680. // Desc: Sets the number of vertices to display on the current progressive mesh
  681. //-----------------------------------------------------------------------------
  682. void
  683. CMyD3DApplication::SetNumVertices(DWORD dwNumVertices)
  684. {
  685.     m_pPMeshFull->SetNumVertices( dwNumVertices );
  686.  
  687.     // if current pm valid for desired value, then set the number of vertices directly
  688.     if ((dwNumVertices >= m_pPMeshes[m_iPMeshCur]->GetMinVertices()) && (dwNumVertices <= m_pPMeshes[m_iPMeshCur]->GetMaxVertices()))
  689.     {
  690.         m_pPMeshes[m_iPMeshCur]->SetNumVertices( dwNumVertices );
  691.     }
  692.     else  // search for the right one
  693.     {
  694.         m_iPMeshCur = m_cPMeshes - 1;
  695.  
  696.         // look for the correct "bin" 
  697.         while (m_iPMeshCur > 0)
  698.         {
  699.             // if number of vertices is less than current max then we found one to fit
  700.             if (dwNumVertices >= m_pPMeshes[m_iPMeshCur]->GetMinVertices())
  701.                 break;
  702.  
  703.             m_iPMeshCur -= 1;
  704.         }
  705.  
  706.         // set the vertices on the newly selected mesh
  707.         m_pPMeshes[m_iPMeshCur]->SetNumVertices( dwNumVertices );
  708.     }
  709.  
  710. }
  711.